package com.iflysse.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iflysse.common.ResultEnum;
import com.iflysse.pojo.bo.resp.SearchResourceListBoResp;
import com.iflysse.pojo.vo.req.ResourceKeyWordVoReq;
import com.iflysse.pojo.vo.req.StudentIdVoReq;
import com.iflysse.pojo.vo.resp.SearchResourceListResp;
import com.iflysse.pojo.vo.resp.SearchResourceListVoResp;
import com.iflysse.services.ResourceService;
import com.iflysse.util.CommonUtil;
import com.iflysse.util.FileUpdateUtil;
import com.iflysse.util.HttpClientUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/studentIntelligent")
@Api(value = "智能接口")
@Log4j
public class StudentIntelligentController{


    @Autowired
   private ResourceService resourceService;

    /**
     * 根据条件智能搜索
     */
    @PostMapping("/queryResourceByCond")
    @ApiOperation("根据条件智能搜索")
    public SearchResourceListVoResp queryResourceByCond(ResourceKeyWordVoReq resourceKeyWordVoReq){

        SearchResourceListVoResp searchResourceListVoResp=new SearchResourceListVoResp();
        List<SearchResourceListResp> result=new ArrayList<>();
        try{

            log.info("根据条件智能搜索参数bean:"+resourceKeyWordVoReq);
            Map<String,String> map=new HashMap<String,String>();
            JSONArray objects=null;
            map.put("key_word",resourceKeyWordVoReq.getKeywords());
            JSONObject jsonObject= HttpClientUtil.postForForm(HttpClientUtil.keyWordSearchUrl,map);
            if((Integer)jsonObject.get("code")==1){
                JSONObject data=(JSONObject) jsonObject.get("data");
                objects=(JSONArray) data.get("objects");
            }
            String studentId=resourceKeyWordVoReq.getStudentId();
            if(objects!=null&&objects.size()>0) {
                for (int i=0;i<objects.size();i++) {
                    SearchResourceListBoResp searchResourceListBoResp = resourceService.getResourceByKeywords(objects.getString(i), studentId);
                    searchResourceListBoResp.getSearchResourceBoRespList().forEach(searchResourceBoResp -> {
                        SearchResourceListResp searchResourceListResp= CommonUtil.objToObj(searchResourceBoResp, SearchResourceListResp.class);
                        searchResourceListResp.setCourseCover(FileUpdateUtil.prefixResources+searchResourceBoResp.getCourseCover());
                        result.add(searchResourceListResp);
                    });
                }
            }

            searchResourceListVoResp.setResult(result);
            searchResourceListVoResp.setCode(ResultEnum.SUCCESS.getCode());
            searchResourceListVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("根据条件智能搜索结果响应bean:"+searchResourceListVoResp);
            return searchResourceListVoResp;

        }catch (Exception e){
            log.error("根据条件智能搜索出现异常："+e);
            CommonUtil.printError(searchResourceListVoResp);
            return searchResourceListVoResp;
        }
    }

    /**
     * 智能推荐
     */
    @PostMapping("/intelligentRecommendation")
    @ApiOperation("智能推荐")
    public SearchResourceListVoResp intelligentRecommendation(StudentIdVoReq studentIdVoReq){

        SearchResourceListVoResp searchResourceListVoResp=new SearchResourceListVoResp();
        try{
            log.info("智能推荐接口参数bean:"+studentIdVoReq);
            /**
             * TODO 等待调用人工智能接口
             */
            //mock 开始
            List<String>resourceIds=new ArrayList<>();
            resourceIds.add("e4315fa2-da29-40d9-aa3e-41799951ba75");
            resourceIds.add("2879facd-3088-4b7a-9953-3c78169490c3");
            resourceIds.add("549f865b-740e-4b29-91f3-d14f5e931d2d");
            resourceIds.add("72237c80-360c-477d-9352-c56b30475a62");
            resourceIds.add("a597a6e3-b07f-4ebb-ba41-482c512dc89d");
            resourceIds.add("1a2f66da-6ef9-485a-b229-46a4baaf2c14");
            resourceIds.add("07f832e2-0f4f-4a6b-95c5-a6b98131b892");
            resourceIds.add("8de3689e-10f3-450a-921c-393a9f23d933");
            //mock结束


            log.info("智能推荐业务接口参数："+resourceIds);
            SearchResourceListBoResp searchResourceListBoResp= resourceService.getResourceByResourceIds(resourceIds);
            log.info("智能推荐业务接口响应bean："+searchResourceListBoResp);
            List<SearchResourceListResp>result=new ArrayList<>();
            searchResourceListBoResp.getSearchResourceBoRespList().forEach(searchResourceBoResp -> {
                SearchResourceListResp searchResourceListResp= CommonUtil.objToObj(searchResourceBoResp, SearchResourceListResp.class);
                searchResourceListResp.setCourseCover(FileUpdateUtil.prefixResources+searchResourceBoResp.getCourseCover());
                result.add(searchResourceListResp);
            });
            searchResourceListVoResp.setResult(result);
            searchResourceListVoResp.setCode(ResultEnum.SUCCESS.getCode());
            searchResourceListVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("智能推荐接口响应bean:"+searchResourceListVoResp);
            return searchResourceListVoResp;

        }catch (Exception e){
            log.error("智能推荐接口出现异常："+e);
            CommonUtil.printError(searchResourceListVoResp);
            return searchResourceListVoResp;
        }

    }


}
